package com.hod.roommap.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;
import com.hod.roommap.client.maps.BedRect;
import com.hod.roommap.client.maps.RoomRect;
import com.hod.roommap.client.model.AbstractGrid;
import com.hod.roommap.client.model.Bed;
import com.hod.roommap.client.model.BedCRUD;
import com.hod.roommap.client.model.BedRecordset;
import com.hod.roommap.client.model.Client;
import com.hod.roommap.client.model.ClientCRUD;
import com.hod.roommap.client.model.ClientRecordset;
import com.hod.roommap.client.model.GearsDataObject;
import com.hod.roommap.client.model.GearsDataStore;
import com.hod.roommap.client.model.HODForm;
import com.hod.roommap.client.model.Lodging;
import com.hod.roommap.client.model.LodgingRecordset;
import com.hod.roommap.client.model.Reservation;
import com.hod.roommap.client.model.ReservationGrid;
import com.hod.roommap.client.model.ReservationRecordset;
import com.hod.roommap.client.model.Room;
import com.hod.roommap.client.model.RoomCRUD;
import com.hod.roommap.client.model.RoomFormPanel;
import com.hod.roommap.client.model.RoomRecordset;
import com.objetdirect.tatami.client.DatePicker;
import com.objetdirect.tatami.client.gfx.Color;
import com.objetdirect.tatami.client.gfx.GraphicCanvas;
import com.objetdirect.tatami.client.gfx.GraphicObject;
import com.objetdirect.tatami.client.gfx.GraphicObjectListener;
import com.objetdirect.tatami.client.gfx.Text;

public class HOD_Roommap extends Composite implements GraphicObjectListener, ClickListener, ChangeListener, EntryPoint{
	private GraphicCanvas canvas;
	private Color currentStrokeColor = Color.BLACK;
	private Color currentFillColor = Color.WHITE;
	private int currentStrokeSize = 1;
	private HODForm formclient = new ClientCRUD().createFormPanel();
	
	
	private void showGraphicObject(GraphicObject object, int x, int y) {
			object.setStroke(currentStrokeColor, currentStrokeSize);			
			object.setFillColor(currentFillColor);			
			this.canvas.add(object, x, y);
	}
	    
	
	public static int aleatorio(int max,int min){
		return (int)(Math.random()*(max-min))+min;		
    }

	public void onModuleLoad() {
		    int x = 0;
		    int y = 0;
		    int x1 = 0;
		    int y1 = 0;
		    double width = 0;
		    double height = 0;
		    int k = 0;
	        try {
	        	
				canvas = new GraphicCanvas();				
				canvas.setPixelSize(1200, 600);
				canvas.addGraphicObjectListener(this);

				Room[] rooms = RoomRecordset.getRoomsByFloor();
				int pisoAnt = -1;
				int i = 0;
				k = 5;
				Client client = null;
				for(Room room:rooms){					
					if (pisoAnt!=room.getFloor()){
					   --k; //piso
					   pisoAnt = room.getFloor();
					   i = 0;
					}
					i++; //ubicacion de la habitacion					
					x1 = 103*i;
					y1 = 105*k;
             		 //hospedaje en la habitacion x para la fecha ini
             		client = LodgingRecordset.existsLodgingforRoom(room.getIdRoom(), new Date());
             		if ((client!=null)||(!room.getState().equals("FREE")))
             			currentFillColor = Color.RED; //ocupada
             		else   
             			currentFillColor = Color.GREEN; //libre
                    currentStrokeColor = Color.WHITE;
					RoomRect roomRect = new RoomRect(100,100,room);				   
					this.showGraphicObject(roomRect,x1, y1);
					this.showGraphicObject(new Text(""+room.getNumber()), x1, 115*k);
					height = roomRect.getHeight()/2;
					if (roomRect.getBeds()>1) {
						   width = roomRect.getWidth() / (roomRect.getBeds()-1);
					}else{
						   width  = roomRect.getWidth()/2;
					}
					   y = y1;
					   x = x1;
					   //listado de camas para la habitacion
					   Bed[] beds = BedRecordset.getBed(room.getIdRoom());
					   int j = 0;
					   
					   for (Bed bed:beds){
	                	   client = LodgingRecordset.existsLodgingforBed(bed.getIdBed(), new Date());
	                	   if ((client!=null)||(!room.getState().equals("FREE")))
	                		  currentFillColor = Color.RED; //ocupada	                			   
	                	   else
	                		  currentFillColor = Color.GREEN; //libre
	                	   
						   currentStrokeColor = Color.WHITE;
						   BedRect bedRect = new BedRect(width,height,bed);
						   bedRect.setRoom(roomRect);
						   if (j==(beds.length/2)){
							   y = y + (int)height;
							   x = x1;
						   }
						   this.showGraphicObject(bedRect,x,y);
						   this.showGraphicObject(new Text(bedRect.getBed().getNumber()+""), (int)bedRect.getX()+10, (int)bedRect.getY()+10);
						   x = x + (int)width;	
						   j++;
					}
					   
				}
				RootPanel.get().add(canvas);
				RootPanel.get().add(formclient);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				com.google.gwt.user.client.Window.alert(e.getMessage());
			}
	}

	@Override
	public void onClick(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onChange(Widget sender) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDblClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		try {
			if (graphicObject instanceof RoomRect){
				RoomRect room = (RoomRect)graphicObject;
				final TabPanel tabPanel = new TabPanel();
				final VerticalPanel panelRoom = new VerticalPanel();
				//final VerticalPanel panelReservation = new VerticalPanel();
				final RoomCRUD roomcrud = new RoomCRUD();
				
				tabPanel.setWidth("400px");
				tabPanel.setHeight("300px");
				panelRoom.setTitle("Room");
				panelRoom.add(roomcrud.loadObjectinFormPanel((GearsDataObject)(room.getRoom())));
				//panelReservation.setTitle("Reservation");
				//GearsDataStore ds = (GearsDataStore) GWT.create(Reservation.class);
				//AbstractGrid gridPanel = new ReservationGrid(false,ds,room.getRoom().getIdRoom().toString());				
				//panelReservation.add(gridPanel);
								
		        tabPanel.setActiveTab(0);
		        tabPanel.add(panelRoom);
		        //tabPanel.add(panelReservation);
		        
	        	Window wndRoom = new Window();
	        	wndRoom.add(tabPanel);
	        	
	        	wndRoom.show();
			}else{
				BedRect bed = (BedRect)graphicObject;
				BedCRUD bedcrud = new BedCRUD();
	        	Window wndBed = new Window();
	        	wndBed.add(bedcrud.loadObjectinFormPanel((GearsDataObject)(bed.getBed())));
	        	wndBed.show();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			com.google.gwt.user.client.Window.alert(e.getMessage());
		}
	}
		

	@Override
	public void mouseMoved(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
		
	}

	@Override
	public void mousePressed(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(GraphicObject graphicObject, Event event) {
		// TODO Auto-generated method stub
		try {
			if (graphicObject instanceof RoomRect){
				RoomRect room = (RoomRect)graphicObject;
			    //hay hospedaje para la habitacion entonces muestro el panel
			    Client client = LodgingRecordset.existsLodgingforRoom(room.getRoom().getIdRoom(), new Date()); 	
			    if (client!=null) 
			    	formclient.setObject(client);
			    else{
	                 client = new Client();
	             	 client.setIdRoom(room.getRoom().getIdRoom());
	             	 client.setIdBed(0);
	             	 formclient.setObject(client);	 
			    }				
			}else{
				if (graphicObject instanceof BedRect){
					BedRect bed = (BedRect)graphicObject;
				    //hay hospedaje para la habitacion entonces muestro el panel
				    Client client = LodgingRecordset.existsLodgingforBed(bed.getBed().getIdBed(), new Date());
				    if (client!=null) formclient.setObject(client);
				    else{
		             	client = new Client();		             		 
		             	client.setIdRoom(bed.getBed().getIdRoom());
		             	client.setIdBed(bed.getBed().getIdBed());
		             	formclient.setObject(client);
				    }
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			com.google.gwt.user.client.Window.alert(e.getMessage());
		}
	}
		
}
